package com.lzy.cloud.permission.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.permission.dtos.ResourceDto;
import com.lzy.cloud.permission.facade.ResourceFacade;
import com.lzy.cloud.permission.request.ResourceRequest;
import com.lzy.cloud.permission.response.ResourceResponse;
import com.lzy.cloud.permission.service.ResourceService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Created by WIN7 on 2018/5/16.
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    @Autowired
    ResourceFacade resourceFacade;

    /**
     * 新增菜单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public Result addResource(@RequestBody(required = false) ResourceRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = resourceService.insert(DozerUtils.map(request, ResourceDto.class));
        Ensure.that(ok).isTrue("00030001");
        return new Result(ok);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}")
    public Result delResource(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        Boolean ok = resourceFacade.deleteById(id);
        Ensure.that(ok).isTrue("00030002");
        return new Result(ok);
    }

    /**
     * 更新菜单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update")
    public Result updateResource(@RequestBody ResourceRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = resourceService.update(DozerUtils.map(request, ResourceDto.class));
        Ensure.that(ok).isTrue("00030003");
        return new Result(ok);
    }

    /**
     * 资源详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public Result detail(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        ResourceDto dto = resourceService.selectByPrimaryKey(id);
        Ensure.that(dto).isNotNull("00030004");
        return new Result(DozerUtils.map(dto, ResourceResponse.class));
    }

    /**
     * 查询菜单列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Result selectList(@RequestBody ResourceRequest request) {
        Ensure.that(request).isNotNull("00010001");
        List<ResourceDto> resourceDtoList = resourceService.selectList(DozerUtils.map(request, ResourceDto.class));
        if (resourceDtoList != null && resourceDtoList.size() > 0) {
            return new Result(DozerUtils.maps(resourceDtoList, ResourceResponse.class));
        }
        return new Result();
    }

    /**
     * 查询该节点的一级子节点
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/child/one")
    public Result selectByParentId(@RequestBody ResourceRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ResourceDto> pageInfo = resourceService.queryResourceByParentId(pager, DozerUtils.map(request, ResourceDto.class));
        if (pageInfo != null && pageInfo.getList().size() > 0) {
            return new Result(DozerUtils.maps(pageInfo.getList(), ResourceResponse.class));
        }
        return new Result();
    }

    /**
     * 获取该用户的资源 - 一级子节点
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/user/child/one")
    public Result selectUserResource(@RequestBody ResourceRequest request) {
        Ensure.that(request.getUserId()).isNotNull("00030005");
        Ensure.that(request).isNotNull("00010001");
        List<ResourceDto> resourceDtoList = resourceFacade.selectUserResource(request.getUserId(), request.getParentId(), request.getStatus(), request.getType());
        if (resourceDtoList != null && resourceDtoList.size() > 0) {
            return new Result(DozerUtils.maps(resourceDtoList, ResourceResponse.class));
        }
        return new Result();
    }

    /**
     * 获取该节点 - 所有子节点
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/child/all")
    public Result selectUserAllResource(@RequestBody ResourceRequest request) {
        Ensure.that(request).isNotNull("00010001");
        List<ResourceDto> resourceDtoList = resourceFacade.selectUserAllResource(request.getUserId(), request.getParentId(), request.getStatus(), request.getType());
        if (resourceDtoList != null && resourceDtoList.size() > 0) {
            return new Result(DozerUtils.maps(resourceDtoList, ResourceResponse.class));
        }
        return new Result();
    }
}
